$(() => {
  // 监听消息传递
  chrome.extension.onMessage.addListener(
    function (req) {
      if (req.action === 'setTagUrl') {
        location.href = req.url
      } else if (req.action === 'progress') {
        if ($('#yhby_progress').length > 0) {
          $('#yhby_progress').val(req.data)
        }
      }
      return true
    }
  )
  
  chrome.runtime.sendMessage({ 'cmd': 'getFactoryInfo' }, function(res) {
    if(res && res.factoryName 
      && (/^https:\/\/item\.jd\.com\/\d+\.html/.test(location.href) 
      || /^https:\/\/[a-z0-9A-Z_]+\.jd\.com\/(advance|view)_search-/.test(location.href)
      || /^https:\/\/detail\.1688\.com\/offer\/\d+\.html/.test(location.href)
      || /^https:\/\/.+\.1688\.com\/page\/offerlist.*\.htm/.test(location.href)
      || /^https:\/\/.+\.tmall\.com\/(search\.htm|category.*\.htm|.+scene=taobao_shop)/.test(location.href)
      || /^https:\/\/detail\.tmall\.com\/item.htm/.test(location.href)
      || /^https:\/\/.+\.taobao\.com\/.+\.htm/.test(location.href)
      || /^https:\/\/item\.taobao\.com\/item.htm/.test(location.href))) {
      // 淘宝详情z-index太大
      if (/^https:\/\/item\.taobao\.com\/item.htm/.test(location.href)) {
        $('#detail').css({'z-index': 99})
        $('#J_SubWrap').css({'z-index': 99})
        $('#J_TabBarWrap').css({'z-index': 100})
      }
        //进度条
      $('body').prepend(`
        <progress value="0" id="yhby_progress" max="100" style="width: 100%;position:fixed;z-index:999">
          您的浏览器不支持progress元素
        </progress>
      `)
      //采集按钮
      $('body').prepend(`<button id="yhby_gather">一键搬家</button>`)
      var fromShopName, forum
      if (/^https:\/\/detail\.tmall\.com\/item.htm/.test(location.href) || /^https:\/\/.+\.tmall\.com\/(search\.htm|category.*\.htm|.+scene=taobao_shop)/.test(location.href)) {
        forum = '天猫'
        fromShopName = $('#shopExtra .slogo-shopname').text()
      } else if (/^https:\/\/detail\.1688\.com\/offer\/\d+\.html/.test(location.href) || /^https:\/\/.+\.1688\.com\/page\/offerlist.*\.htm/.test(location.href)) {
        forum = '1688'
        fromShopName = $('.base-info .company-name').text()
      } else if (/^https:\/\/item\.jd\.com\/\d+\.html/.test(location.href)) {
        forum = '京东'
        fromShopName = $('#crumb-wrap .contact .name').text()
      } else if (/^https:\/\/[a-z0-9A-Z_]+\.jd\.com\/(advance|view)_search-/.test(location.href)) {
        forum = '京东'
        fromShopName = $('.jLogo a').text()
      } else if (/^https:\/\/item\.taobao\.com\/item.htm/.test(location.href)) {
        forum = '淘宝'
        let cardShop = $('#J_ShopInfo .tb-shop-name a').attr('title')
        let normalShop = $('.shop-name-link').text()
        fromShopName = cardShop ? cardShop : normalShop
      } else if (/^https:\/\/.+\.taobao\.com\/.+\.htm/.test(location.href)) {
        forum = '淘宝'
        fromShopName = $('.service-block.first-block .shop-name').text()
      }
      
      $('#yhby_gather').click(function() {
        chrome.runtime.sendMessage({ 'cmd': 'getTemplate', 'url': '/templates/set.html' },function(tempData) {
          layer.open({
            type: 1,
            title: '商品迁移',
            shadeClose: true,
            // shade: false,
            area: ['680px', '600px'],
            content: tempData,
            success:function(layero, index) {
              renderHtml({
                fromShopName: fromShopName,
                toShopName: res.factoryName,
                forum: forum,
                layerIndex: index
              })
            }
          })
        })
      })
    }
  })
})
